﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Smart.Data
{
    /// <summary>
    /// 数据源工厂对象
    /// </summary>
    public class DataSourceFactory
    {
        /// <summary>
        /// 创建数据源
        /// </summary>
        /// <param name="dbProvider"></param>
        /// <param name="connect"></param>
        /// <returns></returns>
        public static IDataSource GetDataSource(string dbProvider, string connect)
        {
            return new DataSource(connect) { Provider = dbProvider };
        }

        /// <summary>
        /// 数据源创建
        /// </summary>
        /// <typeparam name="T">数据源实现类</typeparam>
        /// <param name="model">数据库名称</param>
        /// <param name="user">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public static IDataSource GetDataSource<T>(string model, string user, string pwd)
        {
            return GetDataSource<T>(null, null, model, user, pwd);
        }

        /// <summary>
        /// 数据源创建
        /// </summary>
        /// <typeparam name="T">数据源实现类</typeparam>
        /// <param name="server">服务器</param>
        /// <param name="port">端口</param>
        /// <param name="model">数据库名称</param>
        /// <param name="user">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public static IDataSource GetDataSource<T>(string server, string port, string model, string user, string pwd)
        {
            var ctor = typeof(T).GetConstructor(Type.EmptyTypes);
            var instance = ctor.Invoke(new object[0]) as IDataSource;
            if (!string.IsNullOrEmpty(server))
                instance.Server = server;
            if (!string.IsNullOrEmpty(port))
                instance.Port = port;
            if (!string.IsNullOrEmpty(model))
                instance.Model = model;
            if (!string.IsNullOrEmpty(user))
                instance.UserName = user;
            if (!string.IsNullOrEmpty(pwd))
                instance.Password = pwd;
            return instance;
        }

        /// <summary>
        /// Access 数据源
        /// </summary>
        /// <param name="model">路径</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public static IDataSource Access(string model, string pwd = null)
        {
            return new AccessDataSource(model) { Password = pwd };
        }

        /// <summary>
        /// Excel 数据源
        /// </summary>
        /// <param name="model">路径</param>
        /// <returns></returns>
        public static IDataSource Excel(string model)
        {
            return new ExcelDataSource(model);
        }

        /// <summary>
        /// SqlCe 数据源
        /// </summary>
        /// <param name="model">路径</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public static IDataSource SqlCe(string model, string pwd = null)
        {
            return new SqlCeDataSource(model) { Password = pwd };
        }
    }
}
